﻿using IntelligentDataCollection.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Xml;

namespace IntelligentDataCollection.BUS
{
    public class ParameterBUS
    {
        public List<DeviceInfo> QueryAllDevice()
        {
            List<DeviceInfo> DeviceInfos = new List<DeviceInfo>();
            try
            {
                //创建xml文档
                XmlDocument xmldoc = new XmlDocument();

                //导入指定xml文件
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments = true;//忽略文档里面的注释，注释也是节点，但是无法转换成XmlElement，所以这里不忽略，下面转换就报错
                                               //XmlReader是用liu需要using包起来，读完释放资源，防止占用
                using (XmlReader reader = XmlReader.Create("DeviceInfo.xml", settings))
                {
                    xmldoc.Load(reader);
                }

                //获取根节点
                XmlElement areaxml = xmldoc.DocumentElement;

                //遍历根节点的子节点
                foreach (XmlElement area in areaxml.ChildNodes)
                {
                    DeviceInfo Device = new DeviceInfo();
                    Device.ConnectFlag = false;

                    foreach (XmlElement child in area.ChildNodes)
                    {
                        if (child.Name == "DeviceName")
                        {
                            Device.DeviceName = child.InnerText;
                        }
                        else if (child.Name == "DeviceIP")
                        {
                            Device.DeviceIP = child.InnerText;
                        }
                        else if (child.Name == "DevicePort")
                        {
                            Device.DevicePort = Convert.ToInt32(child.InnerText);
                        }
                        else if (child.Name == "DeviceId")
                        {
                            Device.DeviceId = Convert.ToInt32(child.InnerText);
                        }
                    }
                    DeviceInfos.Add(Device);
                }
            }
            catch
            {

            }
            return DeviceInfos;
        }

        public ResponseMessage UpdateDevice(List<DeviceInfo> infos)
        {
            ResponseMessage message = new ResponseMessage();
            List<DeviceInfo> DeviceInfos = new List<DeviceInfo>();
            try
            {
                //创建xml文档
                XmlDocument xmldoc = new XmlDocument();

                //导入指定xml文件
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments = true;//忽略文档里面的注释，注释也是节点，但是无法转换成XmlElement，所以这里不忽略，下面转换就报错
                                               //XmlReader是用liu需要using包起来，读完释放资源，防止占用
                using (XmlReader reader = XmlReader.Create("DeviceInfo.xml", settings))
                {
                    xmldoc.Load(reader);
                }
                //获取根节点
                XmlElement areaxml = xmldoc.DocumentElement;
                areaxml.RemoveAll();

                XmlNode root = xmldoc.SelectSingleNode("DeviceInfo");//查找<bookstore>  
                if (root != null)
                {
                    foreach (var item in infos)
                    {
                        XmlElement xe1 = xmldoc.CreateElement("Device");//创建一个<book>节点   

                        XmlElement xesub1 = xmldoc.CreateElement("DeviceName");
                        xesub1.InnerText = item.DeviceName;//设置文本节点   
                        xe1.AppendChild(xesub1);//添加到<book>节点中   
                        XmlElement xesub2 = xmldoc.CreateElement("DeviceIP");
                        xesub2.InnerText = item.DeviceIP;
                        xe1.AppendChild(xesub2);
                        XmlElement xesub3 = xmldoc.CreateElement("DevicePort");
                        xesub3.InnerText = item.DevicePort.ToString();
                        xe1.AppendChild(xesub3);
                        XmlElement xesub4 = xmldoc.CreateElement("DeviceId");
                        xesub4.InnerText = item.DeviceId.ToString();
                        xe1.AppendChild(xesub4);

                        root.AppendChild(xe1);//添加到<bookstore>节点中 
                    }


                }

                xmldoc.Save("DeviceInfo.xml");
            }
            catch
            {

            }
            return message;
        }

        public StorageInfo QueryStorage()
        {
            StorageInfo storageInfo = new StorageInfo();
            try
            {
                storageInfo.StorageStart = Convert.ToInt32(AppSettingsHelper.Configuration["StorageStart"]);
                storageInfo.StorageCount = Convert.ToInt32(AppSettingsHelper.Configuration["StorageCount"]);
                storageInfo.Interval = Convert.ToInt32(AppSettingsHelper.Configuration["Interval"]);
            }
            catch
            {

            }
            return storageInfo;
        }

        public ResponseMessage UpdateStorage(StorageInfo storageInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
                string path = baseDirectory + "appsettings.json";
                JObject jObject;
                using (StreamReader reader = new StreamReader(path))
                {
                    using JsonTextReader reader2 = new JsonTextReader(reader);
                    jObject = (JObject)JToken.ReadFrom(reader2);
                    jObject["StorageCount"] = storageInfo.StorageCount;
                    jObject["StorageStart"] = storageInfo.StorageStart;
                    jObject["Interval"] = storageInfo.Interval;
                }
                using StreamWriter textWriter = new StreamWriter(path);
                using JsonTextWriter writer = new JsonTextWriter(textWriter);
                jObject.WriteTo(writer);
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "修改异常";
            }
            return message;
        }
    }
}
